package com.xiaoshuidi.cloud.module.report.service.finance;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.oss.core.OosFileService;
import com.xiaoshuidi.cloud.framework.security.core.LoginUser;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.report.controller.finance.vo.*;
import com.xiaoshuidi.cloud.module.report.convert.finance.CollectHandleConvert;
import com.xiaoshuidi.cloud.module.report.dal.DO.report.*;
import com.xiaoshuidi.cloud.module.report.dal.mapper.report.*;
import com.xiaoshuidi.cloud.module.report.enums.ErrorCodeConstants;
import com.xiaoshuidi.cloud.module.report.framework.security.config.ConfigValues;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;


@Service
@RequiredArgsConstructor
@Slf4j
public class CollectInHandleServiceImpl implements CollectInHandleService {

    private final ReportRentDepositSummaryMapper reportRentDepositSummaryMapper;
    private final ReportReserveSummaryMapper reportReserveSummaryMapper;
    private final ReportFeeSummaryMapper reportFeeSummaryMapper;
    private final ReportCollectionSummaryMapper reportCollectionSummaryMapper;
    private final ReportReceivableCheckMapper reportReceivableCheckMapper;
    private final ConfigValues configValues;
    private final OosFileService oosFileService;

    @Override
    public PageResult<ReportRentDepositlRespVO> pageReportRentDepositSummary(CollectHandleReqVO reqVO) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(loginUser==null){
            throw new ServiceException(ErrorCodeConstants.USER_NOT_LOGIN);
        }
        reqVO.setTenantId(loginUser.getTenantId());
        long num = reportRentDepositSummaryMapper.countByParam(reqVO);
        List<ReportRentDepositSummaryDO> summaryDOList = reportRentDepositSummaryMapper.selectPageByParam(reqVO);
        List<ReportRentDepositlRespVO> list = CollectHandleConvert.INSTANCE.convertList(summaryDOList);
        return new PageResult<>(list,num);
    }

    @Override
    public ExportRespVO reportRentDepositExport(CollectHandleReqVO req) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(loginUser==null){
            throw new ServiceException(ErrorCodeConstants.USER_NOT_LOGIN);
        }
        req.setTenantId(loginUser.getTenantId());
        String fileName =  LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + "租金及押金收款汇总表.xlsx";
        String filePath = configValues.getNfsPath() + File.separator +fileName;
        ExcelWriter excelWriter = EasyExcel.write(filePath, ReportRentDepositExportRespVO.class).build();
        WriteSheet sheet = EasyExcel.writerSheet("sheet1").build();
        List<ReportRentDepositSummaryDO> reportDOList = reportRentDepositSummaryMapper.selectListForExport(req);
        if(CollectionUtils.isEmpty(reportDOList)){
            throw new ServiceException(ErrorCodeConstants.ADVANCE_NOT_EXISTS);
        }
        List<ReportRentDepositExportRespVO> list = CollectHandleConvert.INSTANCE.convertDOList2Export(reportDOList);
        excelWriter.write(list, sheet);
        excelWriter.finish();
        //上传到oos返回URL
        String url = oosFileService.oosUploadFile(new File(filePath));
        ExportRespVO resp = new ExportRespVO();
        if(url.startsWith("http:")){
            url = url.replace("http:","https:");
        }
        resp.setUrl(url);
        resp.setFileName(fileName);
        return resp;
    }

    @Override
    public PageResult<ReportReserveRespVO> pageReserveSummary(CollectHandleReqVO reqVO) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(loginUser==null){
            throw new ServiceException(ErrorCodeConstants.USER_NOT_LOGIN);
        }
        reqVO.setTenantId(loginUser.getTenantId());
        long num = reportReserveSummaryMapper.countByParam(reqVO);
        List<ReportReserveSummaryDO> summaryDOList = reportReserveSummaryMapper.selectPageByParam(reqVO);
        List<ReportReserveRespVO> list = CollectHandleConvert.INSTANCE.convertReserveList(summaryDOList);
        return new PageResult<>(list,num);
    }

    @Override
    public ExportRespVO reserveSummaryExport(CollectHandleReqVO req) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(loginUser==null){
            throw new ServiceException(ErrorCodeConstants.USER_NOT_LOGIN);
        }
        req.setTenantId(loginUser.getTenantId());
        String fileName =  LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + "定金统计表.xlsx";
        String filePath = configValues.getNfsPath() + File.separator +fileName;
        ExcelWriter excelWriter = EasyExcel.write(filePath, ReportReserveExportRespVO.class).build();
        WriteSheet sheet = EasyExcel.writerSheet("sheet1").build();
        List<ReportReserveSummaryDO> reportDOList = reportReserveSummaryMapper.selectListForExport(req);
        if(CollectionUtils.isEmpty(reportDOList)){
            throw new ServiceException(ErrorCodeConstants.ADVANCE_NOT_EXISTS);
        }
        List<ReportReserveExportRespVO> list = CollectHandleConvert.INSTANCE.convertReserveDOList2Export(reportDOList);
        excelWriter.write(list, sheet);
        excelWriter.finish();
        //上传到oos返回URL
        String url = oosFileService.oosUploadFile(new File(filePath));
        ExportRespVO resp = new ExportRespVO();
        if(url.startsWith("http:")){
            url = url.replace("http:","https:");
        }
        resp.setUrl(url);
        resp.setFileName(fileName);
        return resp;
    }


    @Override
    public PageResult<ReportFeeRespVO> pageFeeSummary(CollectHandleReqVO reqVO) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(loginUser==null){
            throw new ServiceException(ErrorCodeConstants.USER_NOT_LOGIN);
        }
        reqVO.setTenantId(loginUser.getTenantId());
        long num = reportFeeSummaryMapper.countByParam(reqVO);
        List<ReportFeeSummaryDO> summaryDOList = reportFeeSummaryMapper.selectPageByParam(reqVO);
        List<ReportFeeRespVO> list = CollectHandleConvert.INSTANCE.convertFeeList(summaryDOList);
        return new PageResult<>(list,num);
    }

    @Override
    public ExportRespVO feeSummaryExport(CollectHandleReqVO req) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(loginUser==null){
            throw new ServiceException(ErrorCodeConstants.USER_NOT_LOGIN);
        }
        req.setTenantId(loginUser.getTenantId());
        String fileName =  LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + "水电费收款汇总表.xlsx";
        String filePath = configValues.getNfsPath() + File.separator +fileName;
        ExcelWriter excelWriter = EasyExcel.write(filePath, ReportFeeExportRespVO.class).build();
        WriteSheet sheet = EasyExcel.writerSheet("sheet1").build();
        List<ReportFeeSummaryDO> reportDOList = reportFeeSummaryMapper.selectListForExport(req);
        if(CollectionUtils.isEmpty(reportDOList)){
            throw new ServiceException(ErrorCodeConstants.ADVANCE_NOT_EXISTS);
        }
        List<ReportFeeExportRespVO> list = CollectHandleConvert.INSTANCE.convertFeeDOList2Export(reportDOList);
        excelWriter.write(list, sheet);
        excelWriter.finish();
        //上传到oos返回URL
        String url = oosFileService.oosUploadFile(new File(filePath));
        ExportRespVO resp = new ExportRespVO();
        if(url.startsWith("http:")){
            url = url.replace("http:","https:");
        }
        resp.setUrl(url);
        resp.setFileName(fileName);
        return resp;
    }


    @Override
    public PageResult<ReportCollectionSummaryRespVO> pageCollectionSummary(CollectHandleReqVO reqVO) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(loginUser==null){
            throw new ServiceException(ErrorCodeConstants.USER_NOT_LOGIN);
        }
        reqVO.setTenantId(loginUser.getTenantId());
        long num = reportCollectionSummaryMapper.countByParam(reqVO);
        List<ReportCollectionSummaryDO> summaryDOList = reportCollectionSummaryMapper.selectPageByParam(reqVO);
        List<ReportCollectionSummaryRespVO> list = CollectHandleConvert.INSTANCE.convertCollectionList(summaryDOList);
        return new PageResult<>(list,num);
    }

    @Override
    public ExportRespVO collectionSummaryExport(CollectHandleReqVO reqVO) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(loginUser==null){
            throw new ServiceException(ErrorCodeConstants.USER_NOT_LOGIN);
        }
        reqVO.setTenantId(loginUser.getTenantId());
        String fileName =  LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + "所有收款汇总表.xlsx";
        String filePath = configValues.getNfsPath() + File.separator +fileName;
        ExcelWriter excelWriter = EasyExcel.write(filePath, ReportCollectionSummaryExportRespVO.class).build();
        WriteSheet sheet = EasyExcel.writerSheet("sheet1").build();
        List<ReportCollectionSummaryDO> reportDOList = reportCollectionSummaryMapper.selectListForExport(reqVO);
        if(CollectionUtils.isEmpty(reportDOList)){
            throw new ServiceException(ErrorCodeConstants.ADVANCE_NOT_EXISTS);
        }
        List<ReportCollectionSummaryExportRespVO> list = CollectHandleConvert.INSTANCE.convertCollectionDOList2Export(reportDOList);
        excelWriter.write(list, sheet);
        excelWriter.finish();
        //上传到oos返回URL
        String url = oosFileService.oosUploadFile(new File(filePath));
        ExportRespVO resp = new ExportRespVO();
        if(url.startsWith("http:")){
            url = url.replace("http:","https:");
        }
        resp.setUrl(url);
        resp.setFileName(fileName);
        return resp;
    }

    @Override
    public PageResult<ReportReceivableCheckRespVO> pageReceivableCheck(CollectHandleReqVO reqVO) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(loginUser==null){
            throw new ServiceException(ErrorCodeConstants.USER_NOT_LOGIN);
        }
        reqVO.setTenantId(loginUser.getTenantId());
        long num = reportReceivableCheckMapper.countByParam(reqVO);
        List<ReportReceivableCheckRespVO> list = reportReceivableCheckMapper.selectRespVoPageByParam(reqVO);
        return new PageResult<>(list,num);
    }

    @Override
    public ExportRespVO receivableCheckExport(CollectHandleReqVO reqVO) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if(loginUser==null){
            throw new ServiceException(ErrorCodeConstants.USER_NOT_LOGIN);
        }
        reqVO.setTenantId(loginUser.getTenantId());
        String fileName =  LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + "应收已收租金核对表.xlsx";
        String filePath = configValues.getNfsPath() + File.separator +fileName;
        ExcelWriter excelWriter = EasyExcel.write(filePath, ReportReceivableCheckExportRespVO.class).build();
        WriteSheet sheet = EasyExcel.writerSheet("sheet1").build();
        List<ReportReceivableCheckRespVO> reportList = reportReceivableCheckMapper.selectRespVoListForExport(reqVO);
        if(CollectionUtils.isEmpty(reportList)){
            throw new ServiceException(ErrorCodeConstants.ADVANCE_NOT_EXISTS);
        }
        List<ReportReceivableCheckExportRespVO> list = CollectHandleConvert.INSTANCE.convertReceivableCheckList2Export(reportList);
        excelWriter.write(list, sheet);
        excelWriter.finish();
        //上传到oos返回URL
        String url = oosFileService.oosUploadFile(new File(filePath));
        ExportRespVO resp = new ExportRespVO();
        if(url.startsWith("http:")){
            url = url.replace("http:","https:");
        }
        resp.setUrl(url);
        resp.setFileName(fileName);
        return resp;
    }
}
